<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<div class="flex items-center justify-between mb-2" v-show="showSearch">
				<div>
					<el-radio-group v-model="state.queryForm.messageTypeId" @change="getDataList(true)">
						<el-radio-button :label="null">全部消息</el-radio-button>
						<el-radio-button label="1">系统公告</el-radio-button>
						<el-radio-button label="2">资费提醒</el-radio-button>
						<el-radio-button label="3">业务通知</el-radio-button>
					</el-radio-group>
				</div>
				<div class="flex">
					<el-input placeholder="请输入标题" v-model="state.queryForm.title" />
					<el-button type="primary" icon="search" class="ml-2" @click="getDataList()">查询</el-button>
				</div>
			</div>
			<div class="flex justify-between mb8" style="width: 100%">
				<div>
					<el-button :disabled="multiple" icon="Delete" type="danger" v-auth="'platform_basicSystemMessages_del'" @click="handleDelete(selectObjs)">
						删除
					</el-button>
					<el-button icon="CircleCheck" type="success" :disabled="multiple">标记已读</el-button>
				</div>
				<right-toolbar v-model:showSearch="showSearch" class="ml10 mr20" @queryTable="getDataList"></right-toolbar>
			</div>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
				@selection-change="selectionChangHandle"
				@sort-change="sortChangeHandle"
			>
				<el-table-column type="selection" width="40" align="center" />
				<el-table-column prop="title" label="标题" show-overflow-tooltip />
				<el-table-column prop="readState" label="状态" width="90">
					<template #default="{ row }">
						<el-tag :type="row.readState === 1 ? 'success' : 'info'"> {{ row.readState === 1 ? '已读' : '未读' }} </el-tag>
					</template>
				</el-table-column>
				<el-table-column prop="messageTypeName" label="消息类型" show-overflow-tooltip width="180" />
				<el-table-column label="操作" width="150">
					<template #default="scope">
						<el-button icon="delete" text type="primary" v-auth="'platform_basicSystemMessages_del'" @click="handleDelete([scope.row.id])"
							>删除</el-button
						>
					</template>
				</el-table-column>
			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>
	</div>
</template>

<script setup lang="ts" name="systemBasicSystemMessages">
import { BasicTableProps, useTable } from '/@/hooks/table';
import { fetchList, delObjs } from '/@/api/platform/basicSystemMessages';
import { useMessage, useMessageBox } from '/@/hooks/message';
// 搜索变量
const showSearch = ref(true);
// 多选变量
const selectObjs = ref([]) as any;
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		messageTypeId: null,
		title: null,
	},
	pageList: fetchList,
});

//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, sortChangeHandle, tableStyle } = useTable(state);

// 多选事件
const selectionChangHandle = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm('此操作将永久删除');
	} catch {
		return;
	}

	try {
		await delObjs(ids);
		getDataList();
		useMessage().success('删除成功');
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>
